//
//  CYApplyRewardVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/2/22.
//  Copyright © 2016年 Lee. All rights reserved.
//
/*!
 *  @brief  申请悬赏赏金
 */

#import "CYApplyRewardVC.h"
#import "CYDiscoveryHttpTool.h"
#import "MBProgressHUD+LE.h"
#import "CYCompanyUserTool.h"
#import "CYLoginHttpTool.h"
#import "CYSimpleLoanM.h"

@interface CYApplyRewardVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextView *comment;//备注
@end

@implementation CYApplyRewardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
}

- (void)initData {
    self.view.width = CYScreeSize.width - 64;
    CGFloat h = CYScreeSize.height/2;
    if (h < 300) {
        h = 300;
    }
    self.view.height = h;
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 5;
    
    self.comment.layer.masksToBounds = YES;
    self.comment.layer.cornerRadius = 5;
    self.comment.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    self.comment.layer.borderWidth = 1;
    
    self.name.delegate = self;
    self.phone.delegate = self;
}

- (void)submit {
    if (self.name.text && self.phone.text && !self.name.text.isEmpty && !self.phone.text.isEmpty) {
        CYUser *user = [CYLoginHttpTool userData].user;
        if (user) {
            NSDictionary *param = nil;
            if (self.comment.text && !self.comment.text.isEmpty) {
                param = @{@"userId": user.userId, @"token": user.token, @"carNumber": self.model.carNumber, @"linkPhone": self.phone.text, @"linkName": self.name.text, @"linkMemo": self.comment.text,@"type":@"0"};
            } else {
                param = @{@"userId": user.userId, @"token": user.token, @"carNumber": self.model.carNumber, @"linkPhone": self.phone.text, @"linkName": self.name.text,@"type":@"0"};
            }
            [MBProgressHUD showMessage:nil toView:self.view];
            [CYDiscoveryHttpTool saveRewordWithParam:param success:^(NSString *message) {
                [MBProgressHUD hideHUDForView:self.view animated:NO];
                [MBProgressHUD showSuccess:message];
                
            } failure:^(NSString *message, BOOL isLogin) {
                [MBProgressHUD hideHUDForView:self.view animated:NO];
                [MBProgressHUD showError:message];
                if (!isLogin) {
                   
                    
                }
            }];
        } else {
            [CYCompanyUserTool logoutWithController:self];
        }
    } else {
        [MBProgressHUD showError:@"请完善信息再提交"];
    }
}

- (IBAction)cancel:(UIButton *)sender {
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)submit:(UIButton *)sender {
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        [self submit];
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@end
